<template>
  <div id="userManager">
    <a-form layout="inline" :model="searchParam" @click="doSearch">
      <a-form-item label="账号">
        <a-input v-model:value="searchParam.userAccount" placeholder="输入账号" allow-clear />
      </a-form-item>
      <a-form-item label="用户名">
        <a-input v-model:value="searchParam.userName" placeholder="输入用户名" allow-clear />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>

    <a-table
      class="userTable"
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="doTableChange"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'name'">
          <a>
            {{ record.name }}
          </a>
        </template>
        <template v-if="column.dataIndex === 'userAvatar'">
          <a-image :src="record.userAvatar ?? null" :width="60" />
        </template>
        <template v-else-if="column.dataIndex === 'userRole'">
          <a-tag v-if="record.userRole === 'admin'" color="green"> 管理员</a-tag>
          <a-tag v-if="record.userRole === 'user'" color="blue"> 用户</a-tag>
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime, 'YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template class="operation" v-else-if="column.key === 'action'">
          <a-button class="button" danger @click="doDelete(record.id)">删除</a-button>
          <a-button class="button" type="primary">编辑</a-button>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { deleteUserUsingPost, getUserPageUsingPost } from '@/api/yonghujiekou.ts'
import { message } from 'ant-design-vue'
import UserVO = API.UserVO
import dayjs from 'dayjs'

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
  },
  {
    title: '用户名',
    dataIndex: 'userName',
  },
  {
    title: '账号',
    dataIndex: 'userAccount',
  },
  {
    title: '用户简介',
    dataIndex: 'userProfile',
  },
  {
    title: '用户头像',
    dataIndex: 'userAvatar',
  },
  {
    title: '角色',
    dataIndex: 'userRole',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]
const dataList = ref<UserVO[]>([])
const total = ref(0)
const searchParam = reactive({
  userName: '',
  userAccount: '',
  current: 1,
  pageSize: 10,
})
//分页参数
const pagination = computed(() => {
  return {
    current: searchParam.current,
    pageSize: searchParam.pageSize,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total: any) => `共${total}条`,
  }
})
// 获取数据
const fetchData = async () => {
  const res = await getUserPageUsingPost({
    ...searchParam,
  })
  if (res.data.code === 0) {
    dataList.value = res.data?.data?.records ?? []
    total.value = res.data?.data?.total ?? 0
  } else {
    message.error('请求失败' + res.data.message)
  }
}
// 分页切换
const doTableChange = (page: any) => {
  searchParam.current = page.current
  searchParam.pageSize = page.pageSize
  fetchData()
}
//搜索
const doSearch = async () => {
  searchParam.current = 1
  await fetchData()
}
const doDelete = async (id: any) => {
  if (!id) {
    return
  }
  try {
    const res = await deleteUserUsingPost({ id })
    if (res.data.code === 0) {
      message.success('删除成功')
      // 删除后重新获取最新的数据
      await fetchData()
    } else {
      message.error('删除失败' + res.data.message)
    }
  } catch (e) {
    message.error('删除失败' + e)
  }
}
// 初始化数据
onMounted(() => {
  fetchData()
})
</script>
<style scoped>
#userManager {
  width: 100%;
  overflow: auto;
}

.userTable {
  height: 77vh;
}

.operation {
  display: flex;
}

.button {
  margin: 5px;
}
</style>
